﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Entity;
using BaseSet.Logic;
using BaseSet.Framework;
using System.Data;

namespace WebSite.Admin.UserControl
{
    public partial class Left : System.Web.UI.UserControl
    {
        private Functions topMenu;
        private FunctionItem currMenu;
        private Users user;
        public Functions TopMenu
        {
            get { return topMenu; }
            set
            {
                topMenu = value;
            }
        }

        public FunctionItem CurrMenu
        {
            get { return currMenu; }
            set
            {
                currMenu = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                user = UserBase.GetCurrentAdminUser();
                litTopMenu.Text = TopMenu.FunctionName;

                FunctionsHelper fHelper = new FunctionsHelper();
                List<Functions> fc = fHelper.GetCurrPowerFun(user);

                List<Functions> leftMenus = new List<Functions>();

                if (fc != null && fc.Count > 0)
                {
                    foreach (Functions fun in fc)
                    {
                        if (fun.ParentID == TopMenu.ID)
                        {
                            leftMenus.Add(fun);
                        }
                    }
                }

                if (leftMenus == null || leftMenus.Count == 0)
                {
                    leftMenus.Add(TopMenu);
                }

                rptFun.DataSource = leftMenus;
                rptFun.DataBind();

            }
        }

        protected void rptFun_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.AlternatingItem && e.Item.ItemType != ListItemType.Item)
            {
                return;
            }

            HiddenField hidFunID = e.Item.FindControl("hidFunID") as HiddenField;
            Repeater rptItem = e.Item.FindControl("rptItem") as Repeater;

            string leftMenuID = hidFunID.Value;

            FunctionItemHelper fiHelper = new FunctionItemHelper();
            List<FunctionItem> fis = fiHelper.GetCurrPowerFunItems(user);

            List<FunctionItem> leftItems = new List<FunctionItem>();

            if (fis != null && fis.Count > 0)
            {
                foreach (FunctionItem fi in fis)
                {
                    if (fi.FunctionID == leftMenuID && fi.IsDisplay == 1)
                    {
                        leftItems.Add(fi);
                    }
                }
            }

            rptItem.DataSource = leftItems;
            rptItem.DataBind();
        }
    }
}